﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    
    public partial class TourInfo : Form
    {
        private int seasId = 0;
        private int currentTour = 0;

        public TourInfo(int _seasId)
        {
            InitializeComponent();
            seasId = _seasId;
        }

        private void checkEditState() {

            if (lStatus.Text != "Тур закрыт")
            {
                if (dgvGames.SelectedRows.Count > 0)
                {
                    edit.Enabled = true;
                }
                else
                {
                    edit.Enabled = false;
                }
            }
        }

        private void dgvGames_SelectionChanged(object sender, EventArgs e)
        {
            checkEditState();
        }

        private void TourInfo_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getCurrentTourOfSeason]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = seasId;

            currentTour = Convert.ToInt32(myCommand.ExecuteScalar());
            ComboboxItem selItem = null;

            conn.Close();

            conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getToursOfSeason]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = seasId;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            while (dataReader.Read())
            {
                ComboboxItem ci = new ComboboxItem((Convert.ToInt32(dataReader["num"])+1).ToString(), Convert.ToInt32(dataReader["id"]));
                if (ci.Value == currentTour)
                    selItem = ci;
                cbTours.Items.Add(ci);    
            }

            dataReader.Close();
            conn.Close();

            if (currentTour != 0)
            {
                cbTours.SelectedItem = selItem; // первый открытый тур
            }
            else
            {
                cbTours.SelectedIndex = 0; // или первый (если все закрыты)
            }

            UpdateTour();

        }

        private void UpdateTour() {

            int st = 0;

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getTour]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = currentTour;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            if (dataReader.Read())
            {
                st = Convert.ToInt32(dataReader["status"]);

                if (st == 1) {
                    lStatus.Text = "Тур открыт";                
                }
                else{
                    lStatus.Text = "Тур закрыт";
                }

                UpdateTourGames();
            }

            conn.Close();

            checkCloseTourState();
        }

        private void UpdateTourGames() {

            dgvGames.Rows.Clear();

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getGamesOfTour]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = currentTour;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            while (dataReader.Read())
            {
                dgvGames.Rows.Add(dataReader[0].ToString(), dataReader[1].ToString() + " - " + dataReader[2].ToString(), dataReader[3].ToString() + " - " + dataReader[4].ToString(), dataReader[5].ToString(), dataReader[6].ToString());
            }

            dataReader.Close();
            conn.Close();

            dgvGames.ClearSelection();

        }

        private void checkCloseTourState() {

            int firstOpenedTourId = 0;

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getCurrentTourOfSeason]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = seasId;

            firstOpenedTourId = Convert.ToInt32(myCommand.ExecuteScalar());

            if (firstOpenedTourId != currentTour)
            {
                closeTour.Enabled = false;
            }
            else {
                closeTour.Enabled = true;
            }
        }

        private void cbTours_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboboxItem ci = (ComboboxItem)cbTours.SelectedItem;
            currentTour = ci.Value;
            UpdateTour();

        }

        private void lStatus_TextChanged(object sender, EventArgs e)
        {
            checkEditState();
        }

        private void closeTour_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[closeTour]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = currentTour;

            myCommand.ExecuteScalar();

            conn.Close();

            UpdateTour();
        }

        private void edit_Click(object sender, EventArgs e)
        {
            int gameId = Convert.ToInt32(dgvGames.SelectedRows[0].Cells["id"].Value);

            GameInfo gi = new GameInfo(gameId);
            gi.ShowDialog();

            UpdateTourGames();
        }
    }

    public class ComboboxItem
    {
        public string Text { get; set; }
        public int Value { get; set; }

        public ComboboxItem(string text, int value)
        {
            Text = text; Value = value;
        }

        public override string ToString()
        {
            return Text;
        }
    }

}
